//
//  ViewController.m
//  SocketClient
//
//  Created by TengFeiLi on 2017/3/1.
//  Copyright © 2017年 Dearcc. All rights reserved.
//

#import "ViewController.h"
#import <CocoaAsyncSocket/GCDAsyncSocket.h>

@interface ViewController ()<GCDAsyncSocketDelegate>
@property (nonatomic,strong) dispatch_queue_t socketQueue;
@property (nonatomic,strong) dispatch_queue_t delegateQueue;
@property (nonatomic,strong) GCDAsyncSocket * socket;
@property (nonatomic,strong) UITextView * textView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.socketQueue = dispatch_queue_create("socketQueue", NULL);
    _delegateQueue = dispatch_get_main_queue();
    self.socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:_delegateQueue socketQueue:_socketQueue];

    NSError * error = nil;
    BOOL connection = [self.socket connectToHost:@"127.0.0.1" onPort:3000 withTimeout:-1 error:&error];

    NSData * data = [@"1234567890" dataUsingEncoding:NSUTF8StringEncoding];

    if(connection){
        [self.socket writeData:data withTimeout:1000 tag:100];
        [self.socket setAutoDisconnectOnClosedReadStream:NO];
    }

    UITextView * textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 300)];
    textView.font = [UIFont systemFontOfSize:20];
    textView.backgroundColor = [UIColor lightTextColor];
    [self.view addSubview:textView];
    self.textView = textView;

    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(50, 350, 100, 40)];
    [button setTitle:@"发送" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor blueColor];
    [button addTarget:self action:@selector(setMessage:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

-(void)setMessage:(UIButton *)button{
    if(self.socket.isConnected){
         NSData * data = [self.textView.text dataUsingEncoding:NSUTF8StringEncoding];
        [self.socket writeData:data withTimeout:1000 tag:2];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
    NSLog(@"%li %@",tag,data);
    NSString* message = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"message is: \n%@",message);
    [self.socket readDataWithTimeout:-1 tag:0];
}

-(void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
    NSLog(@"had write data to stream");
    self.textView.text = @"";
    [self.socket readDataWithTimeout:-1 tag:0];
}

-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(nonnull NSString *)host port:(uint16_t)port{
    NSLog(@"SOCKET DID CONNECTION URL IS %@   %i",host,port);
    [self.socket readDataWithTimeout:-1 tag:0];
}

-(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
    NSLog(@"connection disconnection");
}
@end
